package com.xiaofeng.blog.service.impl;

import com.xiaofeng.blog.common.response.AjaxResult;
import com.xiaofeng.blog.service.PicUploadService;
import com.xiaofeng.blog.util.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * @program: xiaofeng-blog
 * @ClassName PicUploadServiceImpl
 * @description:
 * @author: tyf19970214
 * @create: 2020-10-09 14:08
 * @Version 1.0
 **/
@Service
@Transactional
public class PicUploadServiceImpl implements PicUploadService {

    @Value("${IMAGE_URL}")
    public String IMAGE_URL;

    @Override
    public AjaxResult fileUpload(MultipartFile uploadFile) throws Exception {
        try{
            // 首先接收页面上传的文件
            byte[] content = uploadFile.getBytes();
            // 取出文件的扩展名
            String originalFilename = uploadFile.getOriginalFilename();
            String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //    FastDFSClient fastDFSClient = new FastDFSClient(Constant.FAST_CLIENT_CONF);
            FastDFSClient fastDFSClient = new FastDFSClient("application.properties");
            String url = fastDFSClient.uploadFile(content, ext);
            String src=IMAGE_URL+url;//图片真实路径

        }catch (Exception e){

        }
        return null;
    }


    @Override
    public String fileUploadByStr(MultipartFile uploadFile) throws Exception {
        // 首先接收页面上传的文件
        byte[] content = uploadFile.getBytes();
        // 取出文件的扩展名
        String originalFilename = uploadFile.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        //    FastDFSClient fastDFSClient = new FastDFSClient(Constant.FAST_CLIENT_CONF);
        FastDFSClient fastDFSClient = new FastDFSClient("application.properties");
        String url = fastDFSClient.uploadFile(content, ext);
        String src=IMAGE_URL+url;//图片真实路径
        return src;
    }
}
